<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="530px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="规则类型" prop="ruleType">
            <el-radio-group v-model="form.ruleType">
              <el-radio
                v-for="item in dict.rule_type"
                :key="item.id"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="规则顺序" prop="ruleSort">
            <el-input-number
              v-model="form.ruleSort"
              :step="1"
              :min="1"
              step-strictly
              controls-position="right"
            />
          </el-form-item>
          <!--固定-->
          <template v-if="form.ruleType == '1'" class="fixed">
            <el-form-item label="固定内容" prop="ruleContent">
              <el-input v-model="form.ruleContent" />
            </el-form-item>
          </template>
          <!--日期-->
          <template v-if="form.ruleType == '2'" class="fixed">
            <el-form-item label="日期规则" prop="codingDate.dateFormat">
              <el-select
                v-model="form.codingDate.dateFormat"
                placeholder="请选择"
              >
                <el-option-group
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                >
                  <el-option
                    v-for="op in item.options"
                    :key="op.value"
                    :label="op.label"
                    :value="op.value"
                  >
                    <span style="float: left">{{ op.value }}</span>
                    <span
                      style="float: right; color: #8492a6; font-size: 13px"
                      >{{ op.label }}</span
                    >
                  </el-option>
                </el-option-group>
              </el-select>
            </el-form-item>

            <el-form-item label="替换功能">
              <el-switch
                v-model="form.codingDate.isConvert"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </template>
          <!--SQL-->
          <template v-if="form.ruleType == '3'" class="sql">
            <el-form-item label="SQL内容" prop="ruleContent">
              <el-input
                v-model="form.ruleContent"
                :rows="3"
                type="textarea"
              />
            </el-form-item>
          </template>

          <!--序列号-->
          <template v-if="form.ruleType == '4'" class="sn">
            <el-form-item label="起始值" prop="codingSeries.seriesInit">
              <el-input
                v-model="form.codingSeries.seriesInit"
              />
            </el-form-item>
            <el-form-item label="长度" prop="codingSeries.seriesLength">
              <el-input
                v-model="form.codingSeries.seriesLength"
              />
            </el-form-item>
            <el-form-item label="是否循环">
              <el-switch
                v-model="form.codingSeries.isLoog"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
            <el-form-item
              v-if="form.codingSeries.isLoog == '1'"
              label="循环周期"
              prop="codingSeries.basic"
            >
              <el-radio-group v-model="form.codingSeries.basic">
                <el-radio label="0">年</el-radio>
                <el-radio label="1">月</el-radio>
                <el-radio label="2">日</el-radio>
                <el-radio label="3">订单</el-radio>
              </el-radio-group>
            </el-form-item>
          </template>

          <el-form-item label="描述">
            <el-input
              v-model="form.describes"
              :rows="3"
              type="textarea"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="ruleSort" label="规则顺序" />
        <el-table-column prop="ruleType" label="规则类型">
          <template slot-scope="scope">
            {{ dict.label.rule_type[scope.row.ruleType] }}
          </template>
        </el-table-column>
        <el-table-column prop="ruleContent" label="规则内容" />
        <el-table-column prop="finalContent" label="生成的内容" />
        <el-table-column prop="describes" label="描述" />
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'productCodingRule:edit',
              'productCodingRule:del',
            ])
          "
          fixed="right"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProductCodingRule from "@/api/product/productCodingRule";
// import productCodingDate from '@/api/product/productCodingDate'
// import productCodingSeries from '@/api/product/productCodingSeries'
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
const codingDate = {
  id: null,
  codingRuleId: null,
  dateFormat: null,
  isConvert: "0",
};
const codingSeries = {
  id: null,
  codingRuleId: null,
  seriesInit: null,
  seriesLength: null,
  isLoog: "0",
  basic: "2",
  updateTime: null,
};
const defaultForm = {
  id: null,
  codingId: null,
  ruleType: "1",
  ruleSort: null,
  ruleJson: null,
  ruleContent: null,
  finalContent: null,
  describes: null,
  codingDate: codingDate,
  codingSeries: codingSeries,
};
export default {
  name: "ProductCodingRule",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["rule_type"],
  cruds() {
    return CRUD({
      title: "编码规则",
      url: "api/productCodingRule",
      idField: "id",
      sort: "rule_sort,asc",
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: false,
        reset: false,
        import: false
      },
      queryOnPresenterCreated: false,
      crudMethod: { ...crudProductCodingRule },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "productCodingRule:add"],
        edit: ["admin", "productCodingRule:edit"],
        del: ["admin", "productCodingRule:del"],
      },
      rules: {
        codingId: [
          { required: true, message: "编码ID不能为空", trigger: "blur" },
        ],
        ruleType: [
          { required: true, message: "规则类型不能为空", trigger: "blur" },
        ],
        ruleSort: [
          { required: true, message: "规则顺序不能为空", trigger: "blur" },
        ],
        ruleContent: [
          { required: true, message: "规则内容不能为空", trigger: "blur" },
        ],
        "codingDate.dateFormat": [
          { required: true, message: "日期规则不能为空", trigger: "blur" },
        ],
        "codingSeries.seriesInit": [
          { required: true, message: "序列起始值不能为空", trigger: "blur" },
        ],
        "codingSeries.seriesLength": [
          { required: true, message: "序列长度不能为空", trigger: "blur" },
        ],
        "codingSeries.basic": [
          { required: true, message: "序列循环周期不能为空", trigger: "blur" },
        ],
      },
      typeOptions: [
        {
          value: "1",
          label: "年份",
          options: [
            { value: "yyyy", label: "4位年份" },
            { value: "yy", label: "2位年份" },
          ],
        },
        {
          value: "2",
          label: "月份",
          options: [{ value: "MM", label: "2位月份" }],
        },
        {
          value: "3",
          label: "日期",
          options: [{ value: "dd", label: "2位日期" }],
        },
        {
          value: "4",
          label: "其他",
          options: [
            { value: "yyMMdd", label: "6位年月日" },
            { value: "yyyyMMdd", label: "8位年月日" },
          ],
        },
      ],
      codingId: 0,
      value: true,
    };
  },
  methods: {
    // 刷新 - 之后
    [CRUD.HOOK.afterRefresh](crud, form) {
      // 请求获得的表格数据
      let responseData = crud.data;
      if(responseData.length > 0) {
        for (const item of responseData) {
          this.jsonToObj(item)
        }
      }
      return true;
    },
    // 提交 - 之前
    [CRUD.HOOK.beforeSubmit](crud, form) {
      // 表格数据
      let tableData = crud.data;
      // 提交数据
      let formData = crud.form;
      if (formData.id) { // 编辑
        for (const item of tableData) {
          if (formData.id !== item.id && formData.ruleSort === item.ruleSort) {
            this.$notify.error({
              title: '错误',
              message: '不能有重复顺序的规则！'
            });
            return false;
          }
        }
      } else { // 新增
        for (const item of tableData) {
          if (formData.ruleSort === item.ruleSort) {
            this.$notify.error({
              title: '错误',
              message: '不能有重复顺序的规则！'
            });
            return false;
          }
        }
      }
      return true;
    },
    jsonToObj(data){
      if (data.ruleJson && data.ruleType) {
        if (data.ruleType === '2') {
          data.codingDate = JSON.parse(data.ruleJson)
        } else if (data.ruleType === '4') {
          data.codingSeries = JSON.parse(data.ruleJson)
        }
      }
    },
    queryPage(id) {
      this.codingId = id;
      this.crud.params.codingId = id;
      this.crud.toQuery();
    },
  },
  mounted: function () {
    //获取index页面传递过来的值
    defaultForm.codingId = this.$route.params.id
  },
};
</script>

<style scoped>
</style>
